﻿using SmartParkingClient.Entity;
using SmartParkingClient.IBLL;
using SmartParkingClient.IDAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartParkingClient.BLL
{
    public class FileBLL : IFileBLL
    {
        IFileDal _fileDAL;
        public FileBLL(IFileDal fileDAL)
        {
            _fileDAL = fileDAL;
        }


        public async Task<List<UpgradeFileModel>> GetLocalFileList()
        {
            DataTable datas = await _fileDAL.GetLocalFileList();

            return datas.AsEnumerable().Select(row => new UpgradeFileModel
            {
                FileName = row["file_name"].ToString(),
                FileMd5 = row["file_md5"].ToString(),
                Length = row["file_len"] == null ? 0 : int.Parse(row["file_len"].ToString())
            }).ToList();
        }

        public async Task<List<UpgradeFileModel>> GetServerFileList()
        {
            try
            {
                string fileListJson = await _fileDAL.GetServerFileList();

                return System.Text.Json.JsonSerializer.Deserialize<List<UpgradeFileModel>>(fileListJson);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
